package com.zzyl.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingPlanDto;
import com.zzyl.dto.NursingProjectPlan;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.service.NursingPlanService;
import com.zzyl.vo.NursingPlan;
import com.zzyl.vo.NursingPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description NursingPlanServiceImpl
 * @Author Lwj
 * @Date 2024-08-27-15:50
 */
@Service
public class NursingPlanServiceImpl implements NursingPlanService {
    @Autowired
    private NursingPlanMapper nursingPlanMapper;

    /**
     * 查询所有护理计划
     *
     * @return
     */
    @Override
    public List<NursingPlan> getNursingPlanAllInfor() {
        List<NursingPlan> nursingPlan = (List<NursingPlan>) nursingPlanMapper.select();
        return nursingPlan;
    }

    /**
     * 新增
     *
     * @param nursingPlanDto
     */
    @Override
    public void AddNursingPlan(NursingPlanDto nursingPlanDto) {
        nursingPlanMapper.insertNursingPlan(nursingPlanDto);
        Long plan_id = nursingPlanDto.getId();
        List<NursingProjectPlan> nursingProjectPlan = nursingPlanDto.getProjectPlans();
        // 遍历 nursingProjectPlan 列表，并为每个 NursingProjectPlan 对象设置 plan_id
        for (NursingProjectPlan projectPlan : nursingProjectPlan) {
            projectPlan.setPlanId(Math.toIntExact(plan_id)); // 假设 setPlanId 需要一个整数类型
        }
        nursingPlanMapper.insertNursingProjectPlan(nursingProjectPlan);
    }

    /**
     * 条件分页查询
     *
     * @param name
     * @param pageNum
     * @param pageSize
     * @param status
     * @return
     */
    @Override
    public PageResponse<NursingPlan> projectData(String name, Integer pageNum, Integer pageSize, Integer status) {
        PageHelper.startPage(pageNum, pageSize);
        Page<NursingPlan> page = nursingPlanMapper.selectByPage(name, status);
        page.forEach(n->{
            Long id = n.getId();
            Long count=  nursingPlanMapper.getCount(id);
            n.setCount(Math.toIntExact(count));
        });
        PageResponse<NursingPlan> pageResponse = new PageResponse(page);
        pageResponse.setRecords(page.getResult());
        return pageResponse;
    }

    /**
     * 根据ID查询护理计划
     * @param id 护理计划ID
     * @return 护理计划对象
     */
    @Override
    public NursingPlanVo getById(Long id) {
        return nursingPlanMapper.getById(id);
    }


    /**
     * 修改
     *
     * @param nursingPlanDto
     */
    @Override
    public void reSetPlan(NursingPlanDto nursingPlanDto) {
        nursingPlanMapper.put(nursingPlanDto);
        // 更新 project_plan
        for (NursingProjectPlan projectPlan : nursingPlanDto.getProjectPlans()) {
            nursingPlanMapper.updateProjectPlan(projectPlan);
        }
    }

    /**
     * 删除
     *
     * @param id
     */
    @Override
    public void deletePlan(Long id) {
        // 删除相关的 projectPlans
        nursingPlanMapper.deleteByPlanId(id);
        // 删除 nursingPlan
        nursingPlanMapper.deleteByPlanprojrectId(id);
    }

    /**
     * 启用禁用
     *
     * @param id
     * @param status
     */
    @Override
    public void ResetStatus(Long id, int status) {
        nursingPlanMapper.updateStatus(id, status);
    }
    /**
     * 查询所有护理计划
     * @return 护理计划Dto列表
     */
    @Override
    public List<NursingPlanVo> listAll() {
        return nursingPlanMapper.listAll();
    }

}
